﻿namespace Cubezicles.UI
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Graphics;

    public class MoneyManagerPane : ManagerPane
    {
        private MoneyManager m;

        // Okay button
        Texture2D btn;
        Button okButton;

        SoundEffect successfulPurchase;
        const String SuccessfulPurchaseSound = "Sounds/kaching";

        public MoneyManagerPane(Game1 game, MoneyManager monMan) : base(game)//, xPos, yPos, w, h, title)
        {
            m = monMan;

            int xPos = 75;
            int yPos = 100;
            int w = game.screenW - 150;
            int h = game.screenH - 250;

            PanePosition = new Vector2(xPos, yPos);
            PaneSize = new Vector2(w, h);
            FrameTitle = game.Messages["MoneyPaneTitle"];//"End of Day Report";
        }

        protected override void LoadContent()
        {
            btn = Game.Content.Load<Texture2D>("UI/buttons");
            successfulPurchase = Game.Content.Load<SoundEffect>(SuccessfulPurchaseSound);
            base.LoadContent();
        }

        /// <summary>
        /// displays report and updates current funds.... also does other "end of day stuff"... really this should all be in game....
        /// </summary>
        public void DisplayEODReport()
        {
            // Display report to user
            // Called by clock

            // want to indicate starting day funds or no? int startingDayFunds = CurrentFunds;

            // Get the screen width and height
            int screenH = GraphicsDevice.Viewport.Height;
            int screenW = GraphicsDevice.Viewport.Width;

            // Pane position
            int x = 75;
            int y = 100;

            int lineHeight = 20; // amount to increment y when writing a new line of text

            game.SpriteBatch.Begin();
            DrawFrame();
            //game.drawFrame(x, y, screenW - 150, screenH - 250, "End of Day Report", game.Fonts["status"]);

            // Variable for the color of the text to write to screen
            Color textcolor = game.UIblue;

            x += 20;
            y += 30;

            //Revenue Message
            int revenue = m.GetDailyRevenue();
            string str = "Revenue: $" + revenue;
            game.SpriteBatch.DrawString(game.Fonts["status"], str, new Vector2(x, y), textcolor);

            y += lineHeight;
            // Employee Message
            str = "Number of Employees: " + game.EmployeeManager.NextEmployeeID;
            game.SpriteBatch.DrawString(game.Fonts["status"], str, new Vector2(x, y), textcolor);

            y += lineHeight;
            // Employee Salaries Message
            int salary = m.PayAllEmployees();
            str = "Employee Salaries: $" + salary;
            game.SpriteBatch.DrawString(game.Fonts["status"], str, new Vector2(x, y), textcolor);

            // todo Costs from buying objects and hiring bonuses (both deducted at time of purchase)
            y += lineHeight;
            str = "Other Expenses: $" + m.GetExpenses();
            game.SpriteBatch.DrawString(game.Fonts["status"], str, new Vector2(x, y), textcolor);

            y += lineHeight;
            // Profits
            int profits = m.UpdateCurrentFunds();
            str = "Profits: $" + profits;

            //  display in red text if negative profits
            textcolor = profits < 0 ? Color.Red : game.UIblue;
            game.SpriteBatch.DrawString(game.Fonts["status"], str, new Vector2(x, y), textcolor);


            y += lineHeight;
            // Total Money
            str = "Total: $" + m.CurrentFunds;

            //  display in red text if negative total money
            textcolor = m.CurrentFunds < 0 ? Color.Red : game.UIblue;
            game.SpriteBatch.DrawString(game.Fonts["status"], str, new Vector2(x, y), textcolor);

            y += lineHeight;
            game.SpriteBatch.DrawString(game.Fonts["tiny"], GetDailyWorkerReport(), new Vector2(400, 125), game.UIblue);

            // Okay Button
            okButton = new Button()
            {
                Texture = btn,
                font = game.Fonts["info"],
                text = "Okay",
                xpos = screenW / 2,
                ypos = screenH - 200,
                // Close the panel (by continuing time
                // todo figure out how to encapsulate this in the Clock class
                OnClick = (cont) =>
                {
                    game.time.CurrTimeSpeed = game.time.oldSpeed; // I should save the last speed
                }
            };

            okButton.Update(GraphicsDevice.Viewport, game.ms, game.oldms);
            okButton.Draw(game.SpriteBatch, GraphicsDevice.Viewport);
            game.SpriteBatch.End();

            game.EmployeeManager.ResetAllEmployees(); // todo: this should really be called from game.....
        }

        // testing............ finish THIS!!! ***
        // some repeat code form get daily revenue but meh
        private String GetDailyWorkerReport()
        {
            String str = "--- Today's Employee Report ---\n\n";
            str += "Name       Quality of Work     Time Spent Working \n";
            int spacestotal = 20;
            int numspaces = 0;
            foreach (Employee emp in game.EmployeeManager.Employees)
            {
                str += emp.Name;
                numspaces = spacestotal - emp.Name.Length;
                for (int i = 0; i < numspaces; i++) str += " ";
                str += emp.QualityOfWork + "                  " + emp.GetDailyTimeSpentWorking().ToString("F") + "\n";
                    //str += "Name: " + emp.Name + "  Quality of work: " + emp.QualityOfWork + "\n";
                    //str += "                        Time Spent Working Today: " + emp.GetDailyTimeSpentWorking().ToString("F") + "\n";
                    //str += emp.Name + "         " + emp.QualityOfWork + "         " + emp.GetDailyTimeSpentWorking().ToString("F") + "\n";
            }
 
            return str;
        }
    }
}
